package com.tuanbiao.ui.plan.adapter;

import android.text.TextUtils;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hzjq.data.hzjq.entity.CapRecordEntity;
import com.tuanbiao.R;
import com.tuanbiao.utils.LibCollections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/12 15:03
 */
public class DetonatorRegisterAdapter extends BaseQuickAdapter<CapRecordEntity, BaseViewHolder> {
    private final String TAG = this.getClass().getSimpleName();

    /**
     * 选择对象集合
     */
    private final Map<Integer, CapRecordEntity> mSelectSet = new HashMap<>();

    public DetonatorRegisterAdapter() {
        super(R.layout.item_detonator_plan_provider_column);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, CapRecordEntity entity) {
        boolean selected = mSelectSet.containsKey(holder.getLayoutPosition());
        Timber.i("DetonatorRegisterAdapter#select:" + selected);
        holder.setBackgroundResource(R.id.ll_content, selected ? R.drawable.bg_solid_warning_r_2 : R.drawable.bg_solid_white_stroke_primary_r2);
//        holder.setTextColorRes(R.id.tv_location, getTextColor(selected))
//                .setTextColorRes(R.id.tv_code, getTextColor(selected))
//                .setTextColorRes(R.id.tv_delay, getTextColor(selected))
//                .setTextColorRes(R.id.tv_status, getTextColor(selected));

        holder.setText(R.id.tv_location, getContext().getString(R.string.detonator_register_adapter_location
                , entity.getRowNumber(), entity.getHoleNumber()))
                .setText(R.id.tv_code, entity.getConvertUid())
                .setText(R.id.tv_delay, getContext().getString(R.string.detonator_register_adapter_init_delay, entity.getDelay()));
    }

    @Override
    public void setNewInstance(@Nullable List<CapRecordEntity> list) {
        mSelectSet.clear();
        super.setNewInstance(list);
    }

    public void remove(List<CapRecordEntity> data) {
        for (CapRecordEntity entity : data) {
            mSelectSet.remove(getItemPosition(entity));
            super.remove(entity);
        }

    }

    public void remove(CapRecordEntity entity) {
        mSelectSet.remove(getItemPosition(entity));
        super.remove(entity);
    }

    public void notifyItemChanged(CapRecordEntity entity) {
        notifyItemChanged(getItemPosition(entity));
    }

    @ColorRes
    private int getTextColor(boolean isSelected) {
        return isSelected ? R.color.textWhite : R.color.textPrimary;
    }

    public void toggleSelect(int position) {
        Timber.i(TAG + "#toggleSelect:" + position + ",select:" + mSelectSet.containsKey(position));
        if (mSelectSet.containsKey(position)) {
            mSelectSet.remove(position);
            notifyItemChanged(position);
        } else {
            mSelectSet.clear();
            mSelectSet.put(position, getItem(position));
            notifyDataSetChanged();
        }
    }

    public List<CapRecordEntity> getSelectedData() {
        if (mSelectSet.isEmpty()) {
            return new ArrayList<>(0);
        }
        List<CapRecordEntity> list = new ArrayList<>(LibCollections.size(mSelectSet));
        list.addAll(mSelectSet.values());
        return list;
    }

    public void select(CapRecordEntity capRecordEntity) {
        for (CapRecordEntity entity : getData()) {
            if (TextUtils.equals(capRecordEntity.getConvertUid(), entity.getConvertUid())) {
                mSelectSet.clear();
                mSelectSet.put(getItemPosition(entity), entity);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public int getPosition(CapRecordEntity entity) {
        for (int i = 0; i < getItemCount(); i++) {
            if (TextUtils.equals(getData().get(i).getConvertUid(), entity.getConvertUid())) {
                return i;
            }
        }
        return 0;
    }

    public int getPosition(String uid) {
        for (int i = 0; i < getItemCount(); i++) {
            if (TextUtils.equals(getData().get(i).getConvertUid(), uid)) {
                return i;
            }
        }
        return 0;
    }
}
